/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.genbusiness.logic;

import business.domain.Document;
import business.domain.Response;
import business.domain.Terrorist;
import java.io.FileNotFoundException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.WebServiceRef;

/**
 *
 * @author Johann
 */
@MessageDriven(mappedName = "jms/verifQueue", activationConfig = {
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
})
public class VerifProcessor implements MessageListener {
    @WebServiceRef(CmpServeurCallback.class)
    ICmpServeurCallback serviceClient;
    
    TextManager textManager;

    
    public VerifProcessor() {
        textManager = new TextManager();
        
    }
    
    @Override
    public void onMessage(Message message) {
        if(message instanceof ObjectMessage) {
            ObjectMessage objMsg = (ObjectMessage) message;
            try {
                Document document =(Document) objMsg.getObject();
                Response response = analyseDocument(document);
                
                System.out.println("Traitement du fichier : " + document.getName() + " avec la clé : " + document.getKey());

                //Envoie réponse
                //if(response.getValid())
                notifyCLient(response);

            } catch (JMSException ex) {
                System.out.println("Eurreur JMS : " + ex.getMessage());
                //Logger.getLogger(VerifProcessor.class.getName()).log(Level.SEVERE, null, ex);
            } catch (FileNotFoundException ex) {
                System.out.println("Fichier non trouvé : " + ex.getMessage());
                //Logger.getLogger(VerifProcessor.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                System.out.println("Erreur SQL : " + ex.getMessage());
                //Logger.getLogger(VerifProcessor.class.getName()).log(Level.SEVERE, null, ex);
            } catch (NullPointerException ex) {
                System.out.println("Serveur MySQL non trouvé : " + ex.getMessage());
                //Logger.getLogger(VerifProcessor.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    private Response analyseDocument(Document document) throws FileNotFoundException, SQLException {
        Response response = new Response();
        response.setDocument(document);
        
        //Traitement...
        if(textManager.analyseDocument(document)) {
            response.setTerorist(analyseTerrorist(document));
            //Si mail trouvé on valide le document
            if(!"".equals(response.getTerorist().getMail()) && response.getTerorist().getMail() != null)
                response.setValid(true);
        }
        return response;
    }
    
    private Terrorist analyseTerrorist(Document document) throws FileNotFoundException {
        Terrorist terro = new Terrorist();
        terro.setMail(textManager.getTerroristMail(document));
        return terro;
    }

    private void notifyCLient(Response response) {
        try {
            serviceClient.serviceCallback("GenProject_Java_V1", response.getDocument().getTokenUser(), response.getValid(), response.getDocument().getId(), response.getDocument().getName(), response.getDocument().getKey(), response.getTerorist().getMail(), response.getDocument().getValidityPercent());
        }
        catch(Exception ex) {
            System.out.println("Erreur de retour au service WCF : " + ex.getMessage());
        }
    }
}